package com.source.springmvc2x.viewresolver;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 自己实现 来支持多种模板 jsp freemaker
 * Created by tan on 2017/7/12.
 */
public class MyViewResolver implements ViewResolver {
	private Map<String, ViewResolver> suffixViewResolverMap = new HashMap<String, ViewResolver>();
	private ViewResolver defaultViewResolver = null;

	@Override
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		String viewSuffix = viewName.split(".")[1];
		ViewResolver viewResolver = suffixViewResolverMap.get(viewSuffix);
		if (viewResolver != null) {
			return viewResolver.resolveViewName(viewName, locale);
		} else {
			if (defaultViewResolver != null) {
				return defaultViewResolver.resolveViewName(viewName, locale);
			} else {
				throw new Exception("not found resolver");
			}
		}
	}
}
